/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hathor.networking.sender;

import com.hathor.conf.PropertyLoader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Kenneth
 */
public class BatchSender {

    private String ip;
    private int port;

    public BatchSender(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    public void setIP(String ip) {
        this.ip = ip;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void sendBatch(File batchFile) throws Exception {
        // create socket
        Socket socket = new Socket(ip, PropertyLoader.getMotherShipPort());
        // sendfile
        ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
        System.out.println("Sending...");
        InputStream fis;
        BufferedReader br;
        String line;
        fis = new FileInputStream(batchFile);
        br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
        List<String> batchLines = new ArrayList<String>();
        while ((line = br.readLine()) != null) {
            batchLines.add(line);
        }
        os.writeObject(batchLines);
        os.flush();
        socket.close();
    }
}
